// DlgEditSource.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgEditSource.h"
#include "DlgConfirm.h"

// CDlgEditSource dialog

IMPLEMENT_DYNAMIC(CDlgEditSource, CDialog)

CDlgEditSource::CDlgEditSource(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgEditSource::IDD, pParent)
	, m_szID(_T(""))
	, m_szTitle(_T(""))
	, m_szLink(_T(""))
	, m_szDate(_T(""))
	, m_szNotes(_T(""))
	, m_nMaterial(0)
{
	m_bAdd = FALSE;
	m_nItemID = -1;
	m_nStaffID = -1;
}

CDlgEditSource::~CDlgEditSource()
{
}

void CDlgEditSource::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_ID, m_szID);
	DDX_Text(pDX, IDC_EDIT_TITLE, m_szTitle);
	DDX_Text(pDX, IDC_EDIT_URL, m_szLink);
	DDX_Text(pDX, IDC_EDIT_DATE, m_szDate);
	DDX_Text(pDX, IDC_EDIT_NOTES, m_szNotes);
	DDX_Radio(pDX, IDC_RADIO_MATERIAL_1, m_nMaterial);
}


BEGIN_MESSAGE_MAP(CDlgEditSource, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_OPEN, &CDlgEditSource::OnBnClickedButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, &CDlgEditSource::OnBnClickedButtonDelete)
END_MESSAGE_MAP()


// CDlgEditSource message handlers

BOOL CDlgEditSource::OnInitDialog()
{
	CDialog::OnInitDialog();

	ASSERT( m_nStaffID >= 0 );

	// check for add
	if( m_bAdd )
	{
		// set title
		CString szTitle;
		szTitle.Format( _T("Adding Source:") );
		SetWindowTextW( szTitle );

		// disable delete
		GetDlgItem( IDC_BUTTON_DELETE )->EnableWindow( FALSE );
	}
	else
	{
		ASSERT( m_nItemID >= 0 );

		// load source
		SOURCE_LINK Link;
		appConfig.m_SQL.GetSourceLink( &Link, m_nItemID );

		// set variables
		m_szID.Format( _T("%.8ld"), Link.m_id );
		m_szTitle = Link.m_title;
		m_szLink = Link.m_link;
		m_szDate = Link.m_date_in.Format( appConfig.m_General.szDateFormat );
		m_szNotes = Link.m_notes;
		m_nMaterial = Link.m_material;

		// set title
		CString szTitle;
		szTitle.Format( _T("Edit Source: %.8ld"), m_nItemID );
		SetWindowTextW( szTitle );
	}
	
	// update data
	UpdateData( FALSE );


	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgEditSource::OnOK()
{
	// update data
	UpdateData();

	SOURCE_LINK Link;
	// set variables
	Link.m_id = _tstol( m_szID );
	Link.m_title = m_szTitle;
	Link.m_link = m_szLink;
	Link.m_notes = m_szNotes;
	Link.m_material = m_nMaterial;

	// set staff id
	Link.m_staff_id = m_nStaffID;

	// check it
	if( m_szTitle.IsEmpty() || m_szLink.IsEmpty() )
	{
		AfxMessageBox( _T("You must specify a link address.") );
		return;
	}
	// save it
	appConfig.m_SQL.SetSourceLink( &Link, m_nItemID, m_bAdd );

	// set item id
	m_nItemID = Link.m_id;

	CDialog::OnOK();
}

void CDlgEditSource::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	CDialog::OnCancel();
}

void CDlgEditSource::OnBnClickedButtonOpen()
{
	// update data
	UpdateData();

	// Open link
	ShellExecute( m_hWnd, NULL, m_szLink, NULL, NULL, SW_SHOWNORMAL );
}

void CDlgEditSource::OnBnClickedButtonDelete()
{
	// confirm
	CDlgConfirm dlgConfirm;
	dlgConfirm.m_szAction = _T("WARNING:\n\nThis will permanatly delete this source.\n\nAre you sure you want to continue?");
	if( dlgConfirm.DoModal() != IDOK )
		return;

	// remove request
	appConfig.m_SQL.RemoveRow( CMySQLEx::source_links, m_nItemID );
	m_nItemID = -1;

	// close dialog
	CDialog::OnOK();
}
